//
//  MarkDownTableViewController.m
//  Car
//
//  Created by lanou on 15/9/21.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "MarkDownTableViewController.h"
#import "MarkDownTableViewCell.h"
#import "MarkDownModel.h"
@interface MarkDownTableViewController ()
@property (nonatomic ,strong)NSMutableArray *downArray;
@end

@implementation MarkDownTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [ProgressHUD show:@"正在加载..."];
    [self setUpData];
    [self roloadAndJosnWith:kMarkDown];
}
- (void)setUpData{
    __block MarkDownTableViewController *mark = self;
    __block int a = 1;
    [self.tableView addFooterWithCallback:^{
        a++;
        NSLog(@"%d",a);
        NSString *url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/dealer/pdspecs-pm1-pi110000-c110100-o7-b0-ss0-sp0-p%d-s20-l0-minp0-maxp0.json",a];
        [mark roloadAndJosnWith:url];
        [mark.tableView footerEndRefreshing];
    }];
     [self.tableView registerClass:[MarkDownTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIBarButtonItem *aButton = [[UIBarButtonItem alloc]initWithTitle:@"品牌" style:(UIBarButtonItemStyleDone) target:self action:@selector(aButtonClick:)];
    UIBarButtonItem *bButton = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:(UIBarButtonItemStyleDone) target:self action:@selector(bButtonClick:)];
    UIBarButtonItem *cButton = [[UIBarButtonItem alloc]initWithTitle:@"降价" style:(UIBarButtonItemStyleDone) target:self action:@selector(cButtonClick:)];
    UIBarButtonItem *dButton = [[UIBarButtonItem alloc]initWithTitle:@"二手车" style:(UIBarButtonItemStyleDone) target:self action:@selector(dButtonClick:)];
    NSArray *buttonArray = [[NSArray alloc]initWithObjects:aButton,bButton,cButton,dButton, nil];
    self.navigationItem.leftBarButtonItems = buttonArray;
    
   
}
-(void)roloadAndJosnWith:(NSString *)url{
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *arr = dic[@"result"][@"carlist"];
        for (NSDictionary *Dic in arr) {
            MarkDownModel *model = [MarkDownModel JsonWithDictionary:Dic];
            [model setValuesForKeysWithDictionary:Dic[@"dealer"]];
            [self.downArray addObject:model];
        }
        [self.tableView reloadData];
        [ProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (NSMutableArray *)downArray{
    if (!_downArray) {
        _downArray = [[NSMutableArray alloc]init];
    }
    return _downArray;
}
- (void)aButtonClick:(UIBarButtonItem *)button{
    FindTableViewController *findVC = [[FindTableViewController alloc]init];
    SaleTableViewController *saleVC = [[SaleTableViewController alloc]init];
    IIViewDeckController *deckVC = [[IIViewDeckController alloc]initWithCenterViewController:findVC leftViewController:saleVC];
    deckVC.leftSize = 75;
    [self.navigationController pushViewController:deckVC animated:NO];
    
    
}
- (void)bButtonClick:(UIBarButtonItem *)button{
    ScreenTableViewController *screenVC = [[ScreenTableViewController alloc]init];
    [self.navigationController pushViewController:screenVC animated:NO];
}
- (void)cButtonClick:(UIBarButtonItem *)button{
    MarkDownTableViewController *markDownVC = [[MarkDownTableViewController alloc]init];
    [self.navigationController pushViewController:markDownVC animated:NO];
}
- (void)dButtonClick:(UIBarButtonItem *)button{
    UsedCarTableViewController *usedCarVC = [[UsedCarTableViewController alloc]init];
    [self.navigationController pushViewController:usedCarVC animated:NO];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.downArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MarkDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    [cell setWithModel:self.downArray[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}



@end
